// 开启数据响应服务器，作为终端服务器使用
const http = require("http");
const url = require("url")

http.createServer((req,res)=>{
    if(req.url.includes("/api")){

        if(req.method === "GET"){
            const data = url.parse(req.url,true).query;
            const obj = {
                code: 0,
                method:"get",
                data,
                title:"请求成功，给你的get数据"
            }
            res.write(JSON.stringify(obj));
            res.end();

        }else if(req.method === "POST"){
            let str = ""
            req.on("data",(chunk)=>{
                str += chunk;
            })
            req.on("end",()=>{
                const obj = {
                    code: 1,
                    method:"post",
                    data: str,
                    title:"请求成功，给你的post数据"
                }
                res.write(JSON.stringify(obj));
                res.end();
            })
        }
    }
}).listen(3000,()=>{
    console.log("服务器开启成功")
});